﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC.Nozzle
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LayNozzleCommand : HYNoTranExternalCommand
	{
		public override void FilterProcess()
		{
            base.FilterViewType.Add((ViewType)1);
            base.FilterViewType.Add((ViewType)2);
            base.FilterViewType.Add((ViewType)4);
			base.FilterProcess();
		}

		public override Result HYExecute()
		{
			FormLayNozzle formLayNozzle = new FormLayNozzle();
			if (formLayNozzle.ShowDialog() == DialogResult.OK)
			{
				string name;
				Nozzle nozzle;
				if (formLayNozzle.NozzleType == 0)
				{
					name = "球形喷口";
					nozzle = formLayNozzle.GlobeNozzleData;
				}
				else
				{
					name = "筒形喷口_" + formLayNozzle.CylindricalNozzleData.Count;
					nozzle = formLayNozzle.CylindricalNozzleData;
				}
				FamilySymbol familySymbolByName = this.GetFamilySymbolByName(name, "Nozzle", base.Doc, BuiltInCategory.OST_DuctTerminal);
				SideTerminalConnDuct sideTerminalConnDuct = new SideTerminalConnDuct(base.UiDoc, familySymbolByName, nozzle, formLayNozzle.laySideParam);
				Transaction transaction = new Transaction(base.Doc, "布置族");
				try
				{
					for (;;)
					{
						transaction.Start();
						if (formLayNozzle.Layway == 0)
						{
							sideTerminalConnDuct.ConnSingleNozzle();
						}
						else
						{
							sideTerminalConnDuct.ConnAlongLineSideNozzle();
						}
						transaction.Commit();
					}
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
                    if ((int)transaction.GetStatus() != 3)
					{
						transaction.Commit();
					}
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				catch (Exception)
				{
					transaction.RollBack();
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			return Autodesk.Revit.UI.Result.Cancelled;
		}

		public FamilySymbol GetFamilySymbolByName(string name, string typeName, Autodesk.Revit.DB.Document doc, BuiltInCategory builtInCategory)
		{
			FamilySymbol result = null;
			string text = name + ".rfa";
			string text2 = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), typeName, text);
			Family family = null;
			Transaction transaction = null;
			try
			{
				foreach (Element element in YJKElementFilter.GetElementByCategory(doc, typeof(FamilySymbol), builtInCategory))
				{
					if (element is FamilySymbol && (element as FamilySymbol).Family.Name == text.Substring(0, text.IndexOf(".")))
					{
						result = (element as FamilySymbol);
						return result;
					}
				}
				if (File.Exists(text2))
				{
					transaction = new Transaction(doc, "加载族");
					transaction.Start();
					doc.LoadFamily(text2, out family);
					transaction.Commit();
				}
			}
			catch (Exception)
			{
			}
			finally
			{
                if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			if (family != null)
			{
				using (List<FamilySymbol>.Enumerator enumerator2 = family.GetSymbols().GetEnumerator())
				{
					if (enumerator2.MoveNext())
					{
						result = enumerator2.Current;
					}
				}
			}
			return result;
		}
	}
}
